package com.cema.manage.project.manage;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cema.manage.common.exception.BizException;
import com.cema.manage.common.utils.QiniuSimpleUpload;
import com.cema.manage.framework.web.domain.Message;
import com.cema.manage.project.enums.RedisEnum;
import com.cema.manage.redis.RedisService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.thymeleaf.util.DateUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.UUID;

@RestController
@RequestMapping("/api/fileupload")
public class FileUploadController {
    @Resource
    private RedisService redisService;

    @Autowired
    private QiniuSimpleUpload qiniuSimpleUpload;

    @RequestMapping("/upload")
    public Message upLoad(HttpServletRequest request) {
        try {
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                    request.getSession().getServletContext());
            if (multipartResolver.isMultipart(request)) {
                //将request变成多部分request
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                //获取multiRequest 中所有的文件名
                Iterator iter = multiRequest.getFileNames();

                while (iter.hasNext()) {
                    //一次遍历所有文件
                    MultipartFile file = multiRequest.getFile(iter.next().toString());
                    //上传图片
                    if (file != null) {
                        //调用put方法上传
                        String pathAdress = (String) redisService.get(RedisEnum.FILE_STORAGE_ADDRESS.getKey());
                        String currDate = DateUtil.format(new Date(), "yyyyMMdd");
                        String upResult = qiniuSimpleUpload.upload(file.getBytes(), pathAdress + currDate + "/" + QiniuSimpleUpload.getUUID());
                        JSONObject formatUpResult = JSON.parseObject(upResult);
                        if (formatUpResult.containsKey("key")) {
                            String serverUrl = (String) redisService.get(RedisEnum.FILE_DOWNLOAD_ADDRESS.getKey());
                            String url = serverUrl + "/" + formatUpResult.get("key").toString();
                            return Message.success(url);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Message.error("上传失败");
    }
}
